<template>
  <div class="store-home">
    <search-bar></search-bar>
    <flap-card :data="random"></flap-card>
    <scroll :top="scrollTop" @onScroll="onScroll" ref="scroll">
      <div class="banner-wrapper">
        <div class="banner-img" :style="{backgroundImage: `url('${banner}')`}"></div>
      </div>
      <guess-you-like :data="guessYouLike"></guess-you-like>
      <recommend :data="recommend" class="recommend"></recommend>
      <featured class="featured" :title-text="$t('home.featured')" :btn-text="$t('home.seeAll')" :data="featured"></featured>
      <div class="category-list-wrapper" v-for="(item, index) of categoryList" :key="index">
        <category-book :data="item"></category-book>
      </div>
      <category class="category" :data="categories"></category>
    </scroll>
  </div>
</template>

<script>
import SearchBar from '../../components/home/SearchBar'
import Scroll from '../../components/common/Scroll'
import { storeHomeMixin } from '../../utils/mixin'
import FlapCard from '../../components/home/FlapCard'
import { home } from '../../api/store'
import GuessYouLike from '../../components/home/GuessYouLike'
import Recommend from '../../components/home/Recommend'
import Featured from '../../components/home/Featured'
import CategoryBook from '../../components/home/CategoryBook'
import Category from '../../components/home/Category'

export default {
  name: 'StoreHome',
  components: {
    Category,
    CategoryBook,
    Featured,
    Recommend,
    GuessYouLike,
    FlapCard,
    Scroll,
    SearchBar
  },
  mixins: [storeHomeMixin],
  data () {
    return {
      scrollTop: 94,
      random: null,
      banner: '',
      guessYouLike: null,
      recommend: null,
      featured: null,
      categoryList: null,
      categories: null
    }
  },
  methods: {
    onScroll (offsetY) {
      this.setOffsetY(offsetY)
      // 动态调整滚动区的顶部
      if (offsetY > 0) {
        this.scrollTop = 52
      } else {
        this.scrollTop = 94
      }
      // 刷新滚动区
      this.$refs.scroll.refresh()
    }
  },
  mounted () {
    // 使用api中封装好的home方法，获取数据
    home().then(res => {
      if (res && res.status === 200) {
        const data = res.data
        // 设置随机的索引，算法为随机值✖数据中的长度，向下取整
        const randomIndex = Math.floor(Math.random() * data.random.length)
        // 获取相应的数据
        this.random = data.random[randomIndex]
        this.banner = data.banner
        this.guessYouLike = data.guessYouLike
        this.recommend = data.recommend
        this.featured = data.featured
        this.categoryList = data.categoryList
        this.categories = data.categories
      }
    })
  }
}
</script>

<style lang="stylus" scoped>
  @import '../../assets/styles/global.styl'
  .store-home
    width: 100%
    height: 100%
    .banner-wrapper
      width: 100%
      padding: px2rem(10)
      box-sizing: border-box
      .banner-img
        width: 100%
        height: px2rem(150)
        // 保持图片的大小
        background-repeat: no-repeat
        background-size: 100% 100%
    .recommend
      margin-top: px2rem(20)
    .featured
      margin-top: px2rem(20)
    .category-list-wrapper
      margin-top: px2rem(20)
    .category
      margin-top: px2rem(20)
</style>
